<?php

require_once( 'mvc/mvc-exception.php' );

require_once( 'flexical/model/format.php' );

require_once( 'flexical/admin/object-builder.php' );
require_once( 'flexical/admin/request-factory.php' );
require_once( 'flexical/admin/format-request.php' );

class Flexical_Admin_Format_Request_Factory extends Flexical_Admin_Request_Factory
{
	public function get_request()
	{
		$request = null;

		switch( $this->get_request_type( $_REQUEST ) )
		{
			case 'show_formats':
				$request = new Flexical_Admin_Show_Formats_Request();
				break;

			case 'create_format':
				$request = new Flexical_Admin_Create_Format_Request( $this->build_format( $_REQUEST ) );
				break;

			case 'view_format':
				$request = new Flexical_Admin_View_Format_Request( $this->build_format( $_REQUEST ) );
				break;

			case 'edit_format':
				$request = new Flexical_Admin_Edit_Format_Request( $this->build_format( $_REQUEST ) );
				break;

			case 'update_format':
				$request = new Flexical_Admin_Update_Format_Request( $this->build_format( $_REQUEST ),
					$this->get_cancel_selected( $_REQUEST ) );
				break;

			case 'confirm_delete_format':
				$request = new Flexical_Admin_Confirm_Delete_Format_Request( $this->build_format( $_REQUEST ) );
				break;

			case 'delete_format':
				$request = new Flexical_Admin_Delete_Format_Request( $this->build_format( $_REQUEST ),
					$this->get_cancel_selected( $_REQUEST ) );
				break;

			case 'delete_all_formats':
				$request = new Flexical_Admin_Delete_All_Formats_Request( $this->get_confirmation( $_REQUEST ) );
				break;

			default:
				throw new Exception( "Unknown request type: " . $this->get_request_type( $_REQUEST ) );
		}

		return $request;
	}

	public function build_format( $request )
	{
		$specs = array(
			'format_id'                 => new Flexical_Admin_Object_Property_Spec( 'id', 'int' ),
			'format_title'              => new Flexical_Admin_Object_Property_Spec( 'title', 'string' ),
			'format_start_listing_code' => new Flexical_Admin_Object_Property_Spec( 'start_listing_code', 'string' ),
			'format_event_code'         => new Flexical_Admin_Object_Property_Spec( 'event_code', 'string' ),
			'format_end_listing_code'   => new Flexical_Admin_Object_Property_Spec( 'end_listing_code', 'string' ),
			'format_date_format'        => new Flexical_Admin_Object_Property_Spec( 'date_format', 'string' ),
			'format_time_format'        => new Flexical_Admin_Object_Property_Spec( 'time_format', 'string' ),
		);

		$info = new MVC_Dynamo_Info( 'flexical/model/format.php', 'Flexical_Model_Format' );

		$builder = new Flexical_Admin_Object_Builder( $specs, $info );

		return $builder->build( $request );
	}
}

?>
